class PalacesController < ApplicationController
  # GET /palaces
  # GET /palaces.xml
  def index
    @players = Player.find(:all)
    @player = Player.find(params[:player_id])
    @palaces = @player.palaces

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @palaces }
    end
  end

  # GET /palaces/1
  # GET /palaces/1.xml
  def show
    @players = Player.find(:all)
    @player = Player.find(params[:player_id])
    @palace = Palace.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @palace }
    end
  end

  # GET /palaces/new
  # GET /palaces/new.xml
  def new
    @player = Player.find(params[:player_id])
    @palace = @player.palaces.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @palace }
    end
  end

  # GET /palaces/1/edit
  def edit
    @players = Player.find(:all)
    @player = Player.find(params[:player_id])
    @palace = Palace.find(params[:id])
  end

  # POST /palaces
  # POST /palaces.xml
  def create
    @player = Player.find(params[:player_id])
    @palace = @player.palaces.build(params[:palace])

    respond_to do |format|
      if @palace.save
        flash[:notice] = 'Palaces was successfully created.'
        format.html { redirect_to player_palace_path(@player, @palace) }
        format.xml  { render :xml => @palace, :status => :created, :location => @palace }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @palace.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /palaces/1
  # PUT /palaces/1.xml
  def update
    @player = Player.find(params[:player_id])
    @palace = Palace.find(params[:id])

    respond_to do |format|
      if @palace.update_attributes(params[:palace])
        flash[:notice] = 'Palace was successfully updated.'
        format.html { redirect_to player_palace_path(@player, @palace) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @palace.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /palaces/1
  # DELETE /palaces/1.xml
  def destroy
    @player = Player.find(params[:player_id])
    @palace = Palace.find(params[:id])
    @palace.destroy

    respond_to do |format|
      format.html { redirect_to player_palace_path(@player, @palace) }
      format.xml  { head :ok }
    end
  end

  def berechne_zeit
    @player = Player.find(params[:player_id])
    @palace = Palace.find(params[:palace_id])

    amount = @palace.berechne_zeit(params[:kosten].to_i)

    flash[:result_zeit] = amount

    redirect_to player_palace_path(@player, @palace)
  end

  def berechne_mana
    @player = Player.find(params[:player_id])
    @palace = Palace.find(params[:palace_id])

    amount = @palace.berechne_mana(params[:zeit].to_i)

    flash[:result_mana] = amount

    redirect_to player_palace_path(@player, @palace)
  end
end
